--module("ResourceManager", package.seeall)

local ResourceManager = {} 
--local resourceDefinitions = require "resource_definitions"

ASSETS_PATH  = 'assets/'

local cache = {}

  function ResourceManager:get (name)
    if (not self:loaded(name)) then
      self:load(name)
    end
      return cache[name]
  end
  
  function ResourceManager:loaded (name)
    local res = cache[name]
    return cache[name] ~= nil
  end
  
  function ResourceManager:load(name)
    local resourceDefinition = resourceDefinitions:get(name)
    if not resourceDefinition then
      print("ERROR: resource missing for " .. name)
    else
      local resource
      if (resourceDefinition.type == RESOURCE_TYPE_IMAGE) then
        resource = self:loadImage(resourceDefinition)
      elseif (resourceDefinition.type == RESOURCE_TYPE_TILED_IMAGE) then
        resource = self:loadTiledImage(resourceDefinition)
      elseif (resourceDefinition.type == RESOURCE_TYPE_FONT) then
        resource = self:loadFont(resourceDefintion)
      elseif (resourceDefinition.type == RESOURCE_TYPE_SOUND) then
        resource = self:loadSound(resourceDefinition)
      end
      cache[name] = resource
    end
  end
  
  function ResourceManager:loadImage(definition)
    local image
    local filePath = ASSETS_PATH .. definition.fileName
    if definition.coords then
      image = self:loadGfxQuad2D(filePath, definition.coords)
    else 
      local halfWidth = definition.width / 2
      local halfHeight = definition.height / 2
      image = self:loadGfxQuad2D(filePath, {-halfWidth, -halfHeight, halfWidth, halfHeight})
    end
    return image
  end
  
  function ResourceManager:loadGfxQuad2D(filePath, coords)
    local image = MOAIGfxQuad2D.new()
    image:setTexture(filePath)
    image:setRect(unpack(coords))
    return image
  end
  
  function ResourceManager:loadTiledImage(definition)
    local tiledImage = MOAITileDeck2D.new()
    local filePath = ASSETS_PATH .. definition.fileName
    tiledImage:setTexture(filePath)
    tiledImage:setSize(unpack(definition.tileMapSize))
    return tiledImage
  end
  
  function ResourceManager:loadFont(definition)
    local font = MOAIFont.new()
    local filePath = ASSETS_PATH .. definition.fileName
    font:loadFromTTF(filePath, definition.glyphs, definition.fontSize, definition.dpi)
    return font
  end
  
  function ResourceManager:loadSound(definition)
    local sound = MOAIUntzSound.new()
    local filePath = ASSETS_PATH .. definition.filename
    sound:load(filePath)
    sound:setLooping(definition.loop)
    sound:setVolume(definition.volume)
    return sound
  end
  
  function ResourceManager:unload(name)
    if name then
      cache[name] = nil
    end
  end
  
  
  return ResourceManager
  
  
  
    
      
  
        
    
    
    
  
  
  
  